package com.hyj.boxueguxiangmu.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyj.boxueguxiangmu.R;
import com.hyj.boxueguxiangmu.model.LoginModel;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class RegisteredActivity extends AppCompatActivity {
    EditText mUsername,mPassword,mComfrinPassword;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        mUsername = (EditText) findViewById(R.id.username);
        mPassword = (EditText) findViewById(R.id.password);
        mComfrinPassword = (EditText) findViewById(R.id.password1);
    }

    public void back(View view){
        Intent intent = new Intent(RegisteredActivity.this,LongActivity.class);
        startActivity(intent);
        finish();
    }

    public void login(View view){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Register");
        params.addQueryStringParameter("username", mUsername.getText().toString());
        params.addQueryStringParameter("password", mPassword.getText().toString());
        if (TextUtils.isEmpty(mUsername.getText().toString())){
            Toast.makeText(this,"用户名不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if (TextUtils.isEmpty(mPassword.getText().toString())){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        if (!mUsername.getText().toString().equals(mComfrinPassword.getText().toString())){
            Toast.makeText(this,"两次密码输入不一致",Toast.LENGTH_LONG).show();
            return;
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                LoginModel  loginModel = gson.fromJson(result,LoginModel.class);
                if (loginModel.code==200){
                    Toast.makeText(RegisteredActivity.this,"注册成功",Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(RegisteredActivity.this,"注册失败" +loginModel.msg,Toast.LENGTH_LONG).show();
                }
                Log.i("LongActivity","onSuccess loginModel coce:" +loginModel.code);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("LongActivity","onError result:" +ex.getMessage());
            }

            @Override
            public void onCancelled(Callback.CancelledException cex) {
            }

            @Override
            public void onFinished() {

            }
        });
    }

}


